package utils;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

/**
 * Created by ZQ on 2016/4/23.
 */
public class MySqlDB extends SQLiteOpenHelper{

    private Activity a;
    private  Random random = new Random();
    public MySqlDB(Context context) {
        super(context, "crazyGuess.db", null, 1);
        a = (Activity) context;
    }

    public MySqlDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql =  " create table main_tab(" +
                "   id integer primary key, " +
                "   img_title   text," +
                "   img_path    text," +
                "   img_answer  text," +
                "   all_text      text);";

        db.execSQL(sql);
        insertData(db);

    }

    public void insertData(SQLiteDatabase db){
        String sql = " insert into main_tab(img_title, img_path, img_answer, all_text) values(?, ?, ?, ?) ";
        Log.i("zq",sql);
        try {
            String[] alltxt = a.getAssets().list("img_answer");
            for(int i=0;i<alltxt.length;i++) {
                String path = alltxt[i];
                String txtPath = "img_answer/" + path;
                String imgPath = "img_path/" + path.substring(0,path.lastIndexOf(".")) + ".png";
                BufferedReader br = new BufferedReader(new InputStreamReader(a.getAssets().open(txtPath), "GBK"));
                String line = null;
                int txtLine = 0;
                String imgTitle = null;
                String imgAnswer = null;
                while ((line = br.readLine()) != null) {
                    txtLine++;
                    switch (txtLine) {
                        case 1:
                            imgAnswer = line;
                            break;
                        case 2:
                            imgTitle = line;
                            break;
                    }
                }
                Log.i("zq", imgAnswer+"+++++++++++++++++++++");
                String allText = createAllText(imgAnswer);
                db.execSQL(sql, new Object[]{imgTitle, imgPath, imgAnswer, allText});
                br.close();
                Log.i("zq: ",allText+"...................");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private String createAllText(String imgAnswer) throws Exception {

        StringBuffer sb = new StringBuffer();
        sb.append(imgAnswer);

        while(sb.length()<24) {
            sb.append(create());
        }

        return sb.toString();
    }

    //随机生成一个汉字
    public String create() throws Exception {
        String str = null;
        int hightPos, lowPos; // 定义高低位

        hightPos = (176 + Math.abs(random.nextInt(39)));//获取高位值
        lowPos = (161 + Math.abs(random.nextInt(93)));//获取低位值

        byte[] b = new byte[2];
        b[0] = (new Integer(hightPos).byteValue());
        b[1] = (new Integer(lowPos).byteValue());
        str = new String(b, "GBk");//转成中文

        return str;

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
